﻿using System.Collections.Generic;
using System.IO;
using EzDeploy.Resources;

namespace EzDeploy.Core
{
    public class EmbeddedAssemblyCommand : IEmbeddedAssemblyCommand
    {
        IEmbeddedAssemblyILWriter _ilWriter;

        public Result Run(IEnumerable<DeployGridItem> items, Project project)
        {
            string outfile = Path.Combine(project.DeployedDirectory, Path.GetFileName(project.AssemblyFile));

            if (File.Exists(project.AssemblyFile))
            {
                if(!project.OverWriteFile && File.Exists(outfile))
                    return new Result(ResultType.Error, string.Format(MessageResource.OutputFileAlreadyExist, Path.GetFileName(project.AssemblyFile)));

                return this.IlWriter.Write(project, items);
            }

            return new Result(ResultType.Success, string.Format(MessageResource.MainAssemblyNotFoundFmt, Path.GetFileName(project.AssemblyFile)));
        }

        IEmbeddedAssemblyILWriter IlWriter
        {
            get
            {
                if (_ilWriter == null)
                    _ilWriter = new EmbeddedAssemblyILWriter();
                return _ilWriter;
            }
        }
    }
}


